package com.lagou.handler;

import com.lagou.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;

/**
 * 自定义的业务处理器
 */
@Service
public class UserServiceHandler extends ChannelInboundHandlerAdapter {

    //当客户端读取数据时,该方法会被调用
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        Object iUserService = ServerBootStartConfig.getApplication(IUserService.class);

        RpcRequest rpcRequest = (RpcRequest)msg;
        Class<?> aClass = Class.forName(rpcRequest.getClassName());
        Object o = aClass.newInstance();
        Method method = aClass.getMethod(rpcRequest.getMethodName(), rpcRequest.getParameterTypes());
        Object invoke = method.invoke(o, rpcRequest.getParameters());
        rpcRequest.setParameters(new Object[]{invoke});
        ctx.writeAndFlush(rpcRequest);

    }
}
